package com.ynet.hmds28.config;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.taobao.middleware.logger.Logger;
import com.taobao.middleware.logger.LoggerFactory;
import com.ynet.hm.utils.config.Result;
import com.ynet.hm.utils.config.ResultUtil;
import com.ynet.hm.utils.enums.ExceptionEnum;
import com.ynet.hm.utils.exception.MyException;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
	
	private final static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	@ExceptionHandler(Exception.class)
	@ResponseStatus(HttpStatus.OK)
	public Result<?> handleMethodArgumentNotValidException(Exception e) {

		if (e instanceof MyException){
			MyException myException = (MyException) e;
			LOGGER.error("************【异常代码】{},【异常信息】{}************",myException.getCode(),myException.getMessage());
			return ResultUtil.error(myException.getCode(), myException.getMessage());
		} else {
			LOGGER.error("************【系统异常】{}************",e+"");
			return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
		}
	}  

}  